import { get } from '@/utils/localstorage'
import Vue from 'vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: '/home', // 默认显示首页的二级路由
    children: [
      {
        path: 'home',
        component: () => import('@/views/home')
      },
      {
        path: 'art-cate',
        component: () => import('@/views/article/artcate')
      },
      {
        path: 'art-list',
        component: () => import('@/views/article/artlist')
      },
      {
        path: 'user-info',
        component: () => import('@/views/user/userinfo')
      },
      {
        path: 'user-avatar',
        component: () => import('@/views/user/useravatar')
      },
      {
        path: 'user-pwd',
        component: () => import('@/views/user/userpwd')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login')
  },
  {
    path: '/register',
    component: () => import('@/views/register')
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/register' || to.path === '/login') {
    next()
    return
  }
  const token = get()
  if (!token) {
    ElementUI.Message.warning('身份验证失败,请从新登陆')
    next('/login')
    return
  }
  next()
})
export default router
